<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" width="50%" @close="onClose" @opened="open">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" :loading="loading" @click="dispatchRole">确定</el-button>
        <el-button size="small" @click="onClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getAllRoleListAPI } from '@/api/role'
import { getBaseInfo } from '@/api/user'
import { dispatchRoleToEmployeesAPI } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      required: true
    },
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      checkList: [],
      roleList: [],
      loading: false
    }
  },
  created() {
    this.getAllRoleList()
  },
  methods: {
    onClose() {
      this.$emit('update:showRoleDialog', false)
      this.checkList = []
    },
    async getAllRoleList() {
      const { rows } = await getAllRoleListAPI(this.rolepage)
      this.roleList = rows
    },
    async getTheRoleList() {
      const { roleIds } = await getBaseInfo(this.roleId)
      this.checkList = roleIds
    },
    open() {
      this.getTheRoleList()
    },
    async dispatchRole() {
      this.loading = true
      if (!this.checkList.length) return this.$message.error('请至少分配一个角色')
      await dispatchRoleToEmployeesAPI({ id: this.roleId, roleIds: this.checkList })
      this.loading = false
      this.$message.success('分配角色成功!')
      this.onClose()
    }
  }
}
</script>

<style>

</style>
